package com.xinchuang.service.targetSystem.Impl.damage;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinchuang.comment.R;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.entity.targetSystem.damage.DictionaryTable;
import com.xinchuang.entity.targetSystem.damage.Missile;
import com.xinchuang.mapper.targetSystem.damage.DictionaryTableMapper;
import com.xinchuang.mapper.targetSystem.damage.MissileMapper;
import com.xinchuang.service.targetSystem.damage.IDictionaryTableService;
import com.xinchuang.service.targetSystem.damage.MissileService;
import com.xinchuang.utils.CommonUtil;
import com.xinchuang.vo.MissileVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author chenyanming
 * @program systems-analysis
 * @description 导弹管理
 * @date 2024/05/15
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MissileServiceImpl extends ServiceImpl<MissileMapper, Missile> implements MissileService {

    private final MissileMapper missileMapper;

    private final DictionaryTableServiceImpl dictionaryTableService;

    @Override
    public Result addAndUpdateMissile(MissileVo missile) {
        if (StringUtils.isBlank(missile.getMissileName())) {
            return ResultUtil.error(500, "名称不能为空");
        }
        if (StringUtils.isBlank(missile.getMissileType())) {
            return ResultUtil.error(500, "请选择类型");
        }
        if (StringUtils.isNotBlank(missile.getRangeMax()) && !CommonUtil.isDouble(missile.getRangeMax())) {
            return ResultUtil.error(500, "最大射程,请填写数值类型");
        }
        if (StringUtils.isNotBlank(missile.getRangeMin()) && !CommonUtil.isDouble(missile.getRangeMin())) {
            return ResultUtil.error(500, "最小射程,请填写数值类型");
        }
        if (StringUtils.isNotBlank(missile.getMissileCost()) && !CommonUtil.isDouble(missile.getMissileCost())) {
            return ResultUtil.error(500, "造价,请填写数值类型");
        }
        if (StringUtils.isNotBlank(missile.getCenterFrequency()) && !CommonUtil.isDouble(missile.getCenterFrequency())) {
            return ResultUtil.error(500, "中心频率,请填写数值类型");
        }
        if (StringUtils.isNotBlank(missile.getCommunicationMax()) && !CommonUtil.isDouble(missile.getCommunicationMax())) {
            return ResultUtil.error(500, "通信频率上限,请填写数值类型");
        }
        if (StringUtils.isNotBlank(missile.getCommunicationMin()) && !CommonUtil.isDouble(missile.getCommunicationMin())) {
            return ResultUtil.error(500, "通信频率下限,请填写数值类型");
        }
        try {
            Missile m = new Missile();
            m.setId(missile.getId());
            m.setMissileName(missile.getMissileName());
            m.setMissileType(missile.getMissileType());
            if (StringUtils.isNotBlank(missile.getRangeMax())) {
                m.setRangeMax(Double.parseDouble(missile.getRangeMax()));
            }
            if (StringUtils.isNotBlank(missile.getRangeMin())) {
                m.setRangeMin(Double.parseDouble(missile.getRangeMin()));
            }
            if (StringUtils.isNotBlank(missile.getCenterFrequency())) {
                m.setCenterFrequency(Double.parseDouble(missile.getCenterFrequency()));
            }
            if (StringUtils.isNotBlank(missile.getCommunicationMax())) {
                m.setCommunicationMax(Double.parseDouble(missile.getCommunicationMax()));
            }
            if (StringUtils.isNotBlank(missile.getCommunicationMin())) {
                m.setCommunicationMin(Double.parseDouble(missile.getCommunicationMin()));
            }
            if (StringUtils.isNotBlank(missile.getMissileCost())) {
                m.setMissileCost(Double.parseDouble(missile.getMissileCost()));
            }
            boolean num;
            if (StringUtils.isEmpty(m.getId())) {
                m.setId(UUID.randomUUID().toString().replace("-", ""));
                num = missileMapper.insert(m) > 0;
            } else {
                num = missileMapper.updateById(m) > 0;
            }
            if (num) {
                return ResultUtil.success(null, "操作成功");
            } else {
                return ResultUtil.error(500, "操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, "操作失败");
        }
    }

    @Override
    public Map<String, Object> queryList(Integer pageNum, Integer pageSize, String missileName, String missileType) {
        LambdaQueryWrapper<Missile> wrapper = new LambdaQueryWrapper();
        wrapper.like(StringUtils.isNotBlank(missileName), Missile::getMissileName, missileName);
        wrapper.eq(StringUtils.isNotBlank(missileType), Missile::getMissileType, missileType);
        wrapper.eq(Missile::getDataStatus, 0);
        wrapper.orderByDesc(Missile::getCreateTime);
        Page<Missile> page = missileMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        if (CollectionUtil.isNotEmpty(page.getRecords())) {
            List<DictionaryTable> type = dictionaryTableService.getDictionaryTableList("missile_type");
            if (CollectionUtil.isNotEmpty(type)) {
                page.getRecords().forEach(data -> {
                    for (DictionaryTable dictionaryTable : type) {
                        if (data.getMissileType().equals(dictionaryTable.getValue())) {
                            data.setMissileTypeNmae(dictionaryTable.getLabel());
                            break;
                        }
                    }
                });
            }
        }
        //重写定义分页插件返回参数
        Map<String, Object> map = new HashMap<>();
        map.put("dataList", page.getRecords());
        map.put("total", page.getTotal());
        return map;
    }

    @Override
    public R deleteByIds(String id, boolean deleteStatus) {
        try {
            int num;
            if (deleteStatus) {
                //物理删除
                num = missileMapper.deleteById(id);
            } else {
                //逻辑删除
                num = missileMapper.deleteByIdUpdateStatus(id);
            }
            if (num > 0) {
                return R.success("删除成功");
            } else {
                return R.fail("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("删除失败");
        }
    }

    @Override
    public List<Map> getList() {
        List<Missile> missileList = missileMapper.selectList(new QueryWrapper<Missile>().lambda()
                //只返回需要的字段
                .select(Missile::getId, Missile::getMissileName)
                .eq(Missile::getDataStatus, 0));
        List<Map> mapList = missileList.stream().map(missile -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", missile.getId());
            map.put("label", missile.getMissileName());
            return map;
        }).collect(Collectors.toList());
        return mapList;
    }
}
